package team.bailan.service.impl;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.util.WebUtils;
import team.bailan.common.config.ReturnResponse;
import team.bailan.common.enums.ReturnStatus;
import team.bailan.common.util.JwtUtil;
import team.bailan.entity.domain.User;
import team.bailan.entity.dto.UserDTO;
import team.bailan.mapper.UserMapper;
import team.bailan.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author nwh
 * @since 2022-05-16
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    public UserMapper userMapper;

    @Override
    public ReturnResponse<String> register(UserDTO userDTO) {
        try {
            String username = userDTO.getUsername();
            String password = userDTO.getPassword();
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setBeans(5000);
            user.setGivenCount(1);
            if (userMapper.selectOne(new QueryWrapper<User>().eq("username", username)) != null) {
                return ReturnResponse.packageObject("该用户名已存在", ReturnStatus.FAILURE);
            }
            userMapper.insert(user);
            return ReturnResponse.OK("注册成功");
        } catch (Exception e) {
            return ReturnResponse.systemException(ReturnStatus.BUSINESS_EXCEPTION);
        }
    }

    @Override
    public ReturnResponse<String> login(HttpServletRequest request, HttpServletResponse response, @RequestBody UserDTO userDTO) {
        try {
            String username = userDTO.getUsername();
            String password = userDTO.getPassword();
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper
                    .eq("username", username);
            User user = userMapper.selectOne(queryWrapper);
            if (user == null) {
                return ReturnResponse.packageObject("用户名不存在", ReturnStatus.FAILURE);
            } else {
                if (!user.getPassword().equals(password)) {
                    return ReturnResponse.packageObject("密码错误", ReturnStatus.FAILURE);
                } else {
                    String token = JwtUtil.createToken(user);
                    WebUtils.setSessionAttribute(request, username, user.getId());
                    return ReturnResponse.OK(token);
                }
            }
        } catch (Exception e) {
            return ReturnResponse.systemException(ReturnStatus.BUSINESS_EXCEPTION);
        }
    }

    @Override
    public void updateBeans(String[] usernameList, Integer[] userBeans) {
        for (int i = 1; i <= 3; i++) {
            System.out.println(Arrays.toString(usernameList));
            User user = userMapper.selectOne(new QueryWrapper<User>().eq("username", usernameList[i]));
            user.setBeans(userBeans[i]);
            userMapper.updateById(user);
        }
    }
}
